package main

import (
	"fmt"
	"math/rand"
	"time"
)

const (
	PI = 3.1415926
	_  = iota
	a1 = iota
	a2
	a3
	a4
)

var (
	name string
	age  int
)

func main() {
	fmt.Println("hello world!")

	fmt.Println(PI, a1, a2, a3, a4)
	name = "奉志强"
	age = 24
	fmt.Println(name, age)

	gender := "男"
	if age < 18 {
		fmt.Println(gender)
	} else {
		fmt.Println("年纪太小了！")
	}
	// 基本格式
	for i := 0; i < 10; i++ {
		fmt.Println(i)
	}
	// 变种1
	var i = 5
	for ; i < 10; i++ {
		fmt.Println(i)
	}
	// 无限循环
	//for {
	//
	//}

	s2 := "奉志强"
	s3 := []rune(s2) // 把字符串强制转换为一个rune切片(rune => int32 类型)
	s3[0] = '郑'
	fmt.Println(string(s3))

	s := "123安居客多好的哦"
	for _, v := range s {
		fmt.Printf("%c\n", v)
	}
	n := 3
	switch n {
	case 1:
		fmt.Println("1")
	case 2:
		fmt.Println("2")
	case 3:
		fmt.Println("3")
	case 4:
		fmt.Println("4")
	default:
		fmt.Println("no")
	}

	// 定义数组
	var a2 [2]bool
	var d = [5]int{1: 1, 2: 3}
	a1 := [3]int{}
	number := [...]int{1, 2, 3, 4}
	c := [5]int{2: 100, 4: 200}
	fmt.Println(a1, a2, number, c, d)
	for _, v := range c {
		fmt.Println(v)
	}
	stu := [...]struct {
		name string
		age  int
	}{
		{"fzq", 24},
		{"zyy", 18},
	}
	fmt.Println(stu)
	a := [2]int{}
	fmt.Printf("a: %p\n", &a)
	test(a)
	fmt.Println("a的值是否变化：", a)
	fmt.Println(cap(a), len(a))

	// 多维数组
	var b1 = [2][3]int{{0: 2}, {2: 2}}
	b2 := [...][3]int{{12, 2, 13}, {2, 3, 18}, {3, 5, 4}}
	b3 := [2][1]int{}
	fmt.Println(b1)
	fmt.Println(b2)
	fmt.Println(b3)

	for k1, v1 := range b2 {
		for k2, v2 := range v1 {
			fmt.Printf("(%d,%d)=%d ", k1, k2, v2)
		}
		fmt.Println()
	}

	aa := [...]int{1, 2}
	printArr(&aa)
	fmt.Println(aa)

	// 求随机数组所有元素之和
	// 若想做一个真正的随机数，要种子
	// seed()种子默认是1
	//rand.Seed(1)
	rand.Seed(time.Now().Unix())
	arrSum := [10]int{}
	for i := 0; i < len(arrSum); i++ {
		// 产生一个0到1000随机数
		arrSum[i] = rand.Intn(1000)
	}
	fmt.Println("求得的和为：", sum(arrSum))

	// 找出数组中和为给定值的两个元素的下标
	arr := [5]int{1, 3, 5, 8, 7}
	findSum(arr, 8)
	fmt.Println()

	// 对底层数组arr切片
	ss := arr[0:4]
	fmt.Println("切片得到：", ss)
	// 对于sss来说底层数组变为ss
	sss := ss[1:]
	fmt.Println(sss)

	data := [...]int{0, 1, 2, 3, 4, 5}
	slice := data[1:4]
	slice[0] += 100
	slice[2] += 300
	fmt.Println(slice)
	// 更改切片的值，直接更改底层数组中的值
	fmt.Println(data)
}

func test(x [2]int) {
	fmt.Printf("x: %p\n", &x)
	x[1] = 1
}

func printArr(arr *[2]int) {
	arr[0] = 10
	for i, v := range arr {
		fmt.Println(i, v)
	}
}

func sum(arr [10]int) int {
	sum := 0
	for i := 0; i < len(arr); i++ {
		sum += arr[i]
	}
	return sum
}

func findSum(arr [5]int, target int) {
	for i := 0; i < len(arr); i++ {
		other := target - arr[i]
		for j := i + 1; j < len(arr); j++ {
			if arr[j] == other {
				fmt.Printf("(%d,%d)", i, j)
			}
		}
	}
}
